www.gusucode.com > 24Beta 虚拟主机版 1.0.0 Beta源码程序 > 24Beta 虚拟主机版 1.0.0 Beta源码程序/24Beta-1.0.0-vhost/protected/controllers/PostController.php
<?php class PostController extends CController { /** * @return array action filters */ public function filters() { return array( 'accessControl', // perform access control for CRUD operations array( 'COutputCache + list', 'duration' => param('expireCategoryPost'), 'varyByParam' => array('category', 'page'), ), array( 'COutputCache + show', 'duration' => param('expirePost'), 'varyByParam' => array('aid'), ), ); } /** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */ public function accessRules() { return array( array('allow', // allow all users to perform 'list' and 'show' actions 'actions'=>array('list', 'show', 'ajaxMark', 'create', 'ajaxCreate', 'ajaxUpdateVisitNums', 'ajaxDigg'), 'users'=>array('*'), ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=>array('delete'), 'users'=>array('admin'), ), array('deny', // deny all users 'users'=>array('*'), ), ); } /** * Shows a particular model. */ public function actionShow() { $post = $this->loadPost(); $ads = Advertisement::model()->getAllAvlidTokenIsKey(); $this->render('show', array( 'post' => $post, 'ads' => $ads, )); } /** * ajax mark * @return unknown_type */ public function actionAjaxMark() { //sleep(3); if (!app()->request->isAjaxRequest || !app()->request->isPostRequest) { echo '非法请求'; exit; } $fields = array('mark1', 'mark2'); $field = trim($_POST['field']); if (!in_array($field, $fields)) { echo '非法请求'; exit; } $aid = trim($_POST['aid']); $mark = trim($_POST['mark']); $post = Post::model()->findByPk($aid); $field_nums = $field . '_nums'; $field_avgmark = 'avg' . $field; $post->$field += $mark; $post->$field_nums += 1; $result = $post->update(); $data['avgmark'] = $post->$field_avgmark; if ($result) { $data['errno'] = 0; $data['note'] = sprintf('%s(%d 次打分) <span class="color-green">谢谢参与</span>', $post->$field_avgmark, $post->$field_nums); } else { $data['errno'] = 1; $data['note'] = '<span class="color-red">评分错误</a>'; } echo json_encode($data); } public function actionAjaxDigg() { //sleep(1); if (!app()->request->isAjaxRequest || !app()->request->isPostRequest) { echo '非法请求'; exit(0); } $postId = (int)$_GET['pid']; $postMarkAttr = array( 'post_id' => $postId, 'mark_ip' => CdcBetaTools::getClientIp(), ); if (!param('isStrictDiggMode')) $postMark['mark_user'] = user()->isGuest ? app()->session->sessionId : user()->name; $postMark = PostMark::model()->findByAttributes($postMarkAttr); if ($postMark) { $data['code'] = 2; echo json_encode($data); exit(0); } $result = Post::model()->updateCounters(array('digg_nums' => 1), "id = $postId"); if ($result) { $data['code'] = 1; $attr = array( 'post_id' => $postId, 'mark' => PostMark::MARK_DIGG, ); $postMark = new PostMark($attr); $postMark->save(); } else { $data['code'] = -1; } echo json_encode($data); exit(0); } /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'show' page. */ public function actionCreate() { if (app()->request->isPostRequest) { $clientCode = trim($_POST['validateCode']); if (!user()->checkAccess('noValidateCode') && !CdcBetaTools::validateCode($clientCode, true)) { $result = 0; $note[] = array('text' => '验证码不正确,请勿重复提交。', 'url' => 'javascript:history.back();'); $this->render('/site/prompt', array('result' => $result, 'note' => $note)); exit(0); } $subject = trim($_POST['subject']); $category = (int)$_POST['category']; $topic = (int)$_POST['topic']; $summary = trim($_POST['summary']); $content = trim($_POST['content']); if (empty($subject) || empty($category) || empty($topic) || empty($summary) || empty($content)) { $result = 0; $note[] = array('text' => '文章主题、分类、主题、概述和内容都不能为空。', 'url' => 'javascript:history.back();'); $this->render('/site/prompt', array('result' => $result, 'note' => $note)); exit(0); } $post = new Post(); $post->subject = $subject; $post->category_id = $category; $post->topic_id = $topic; $post->summary = $summary; $post->content = $content; $post->contributor = trim($_POST['contributor']); $post->contributor_email = trim($_POST['email']); $post->contributor_homepage = trim($_POST['homepage']); $post->source = trim($_POST['source']); $post->isoriginal = (int)$_POST['isoriginal']; if($post->save()) { $result = 1; $note[] = array('text' => '投递文章成功,感谢您的支持。'); $note[] = array('text' => '继续投递文章', 'url' => url('post/create')); $note[] = array('text' => '查看发表的文章', 'url' => url('post/show', array('aid' => $post->id))); } else { $result = 0; $note[] = array('text' => '投递文章错误,请返回重试。', 'url' => 'javascript:history.back();'); } $this->render('/site/prompt', array('result' => $result, 'note' => $note)); exit(0); } $categorys = Category::model()->getIdNamePairs(); $topics = Topic::model()->getIdNamePairs(); $this->render('create',array( 'categorys' => $categorys, 'topics' => $topics, )); } /** * Updates a particular model. * If update is successful, the browser will be redirected to the 'show' page. */ public function actionUpdate() { $model=$this->loadPost(); if(isset($_POST['Post'])) { $model->attributes=$_POST['Post']; if($model->save()) $this->redirect(array('show','id'=>$model->id)); } $this->render('update',array('model'=>$model)); } public function actionList() { if (app()->request->isAjaxRequest) $this->ajaxLoadPostList(); else $this->loadPostList(); } /** * Lists all models. */ public function loadPostList() { /* * 获取所有有效的文章分类 */ $categorys = Category::model()->getShowCategorys(); /* * 获取最新的文章列表,文章数量以变量替换,换生成$pagination分布对象 */ $category_id = (int)trim($_GET['category']); $activePosts = Post::model()->getActivePosts(param('listPostsNums'), $category_id); /* * 获取访问量最高的新闻列表,条数以变量替换 */ $maxVisit = Post::model()->getTopVisit($category_id, null, param('topPostsNums')); /* * 获取评论数量量最多的新闻列表,条数以变量替换 */ $maxComment = Post::model()->getTopComment($category_id, null, param('topPostsNums')); /* * 获取编辑推荐的新闻列表,条数以变量替换 */ $editorRecommend = Post::model()->getEditorRecommend($category_id, null, param('editorRecommendNums')); /* * 获取热门文章 */ $hotPosts = Post::model()->getHotPosts(param('hotPostsNums')); /* * 获取编辑推荐的评论,条数以变量替换 */ $recommendComment = Comment::model()->getRecommendComment(param('recommendCommentNums')); $ads = Advertisement::model()->getAllAvlidTokenIsKey(); $friendLinks = FriendLink::model()->getAllFriendLinks(FriendLink::YES); $data = array( 'categorys' => $categorys, 'hotPosts' => $hotPosts, 'maxVisit' => $maxVisit, 'maxComment' => $maxComment, 'editorRecommend' => $editorRecommend, 'recommendComment' => $recommendComment, 'ads' => $ads, 'friendLinks' => $friendLinks, ); $data = array_merge($data, $activePosts); $this->render('list', $data); } /** * ajax Lists all models. */ public function ajaxLoadPostList() { $category_id = (int)trim($_GET['category']); $activePosts = Post::model()->getActivePosts(param('listPostsNums'), $category_id); $ads = Advertisement::model()->getAllAvlidTokenIsKey(); $data = array( 'ads' => $ads, ); $data = array_merge($data, $activePosts); $this->renderPartial('/_public/post_list', $data); } /** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable */ public function loadPost($id = null) { if ($id !== null || isset($_GET['aid'])) { $postModel = Post::model(); $data = $postModel->with('category', 'topic')->findbyPk($id !== null ? $id : $_GET['aid']); } if ($data === null) throw new CHttpException(404, 'The requested page does not exist.'); return $data; } public function actionAjaxUpdateVisitNums() { if (!app()->request->isAjaxRequest || !app()->request->isPostRequest) { echo '非法请求'; exit; } $postId = (int)$_POST['postid']; $result = Post::model()->updateCounters(array('visit_nums' => param('visitNumsStep')), "id = $postId"); echo (int)$result; } }